<?php

//
// Returns an association between temperature levels and colors
// 
function get_temp_colors()
{
	$colors = array();
	// The lowest possible color is blue
	$colors["LOWEST"] = $tempColor = "#0066AA";
	// Anything >= 70 degrees is default color
	$colors[70] = "";
	// Anything >= 79 degrees is red
	$colors[79] = "#D33";
	return $colors;
}

//
// Given a temperature and color association, return the corresponding color
//
function assign_temp_color($colors, $temp)
{
	$color = $colors["LOWEST"];
	foreach($colors as $t=>$c)
	{
		if($t === "LOWEST") continue;
		if($temp >= $t)
		{
			$color = $c;
		}
	}
	return $color;
}

//
// Returns the battery level icon depending on battery level
//
function get_icon_of_battery_level($level)
{
	// >= 60% = full icon
  // >= 30% and < 60% = medium icon
  // < 30% = low icon
	if($level >= 60)
		return "icons/batt_full.png";
	else if($level >= 30)
		return "icons/batt_med.png";
	else
		return "icons/batt_low.png";
}

//
// Builds an association of Sensor ID and their battery levels
//
function get_sensor_battery_levels($database)
{
	// First get all active sensor IDs
	$sensors = array();
	$results = $database->multi_query("CALL getActiveSensors()");
	if($results)
	{
		$results = $database->use_result();
		while($row = $results->fetch_row())
		{
				$sensors[] = $row[0];
		}
		$results->free();
		if($database->more_results())
  			while($database->next_result());
	}
	// Now, generate association of sensorID=>battery level
	$battery = array();
	foreach($sensors as $id)
	{
		$results = $database->multi_query("CALL getBatteryStatus($id)");
		if($results)
		{
			$results = $database->use_result();
			while($row = $results->fetch_row())
			{
				$battery[$id] = $row[0];
			}
			$results->free();
			if($database->more_results())
				while($database->next_result());
		}
	}
	return $battery;
}

// 
// Sends a confirm code to a cell phone
//
function sms_confirm_code($number, $provider, $code)
{
	$number = add_sms_gateway($number,$provider);
	mail($number, "iRoomMonitor", "Confirmation Code:\n".$code);
}

//
// Registers a phone number for the SMS notification service
//
function register_phone_number($number, $provider)
{
	// Attach provider domain to number
	$number = add_sms_gateway($number,$provider);
	// Check if number is already registered
	$file = fopen("/var/iroom/phno", 'r');
  while (($buffer = fgets($file)) !== false) 
	{
     	if($number."\n" == $buffer)
			{
				return;
			}
  }  
	fclose($file);
	// It is not, append it to the file
	$file = fopen("/var/iroom/phno",'a')or die("Can't open file!");
	fwrite($file, $number."\n");
	fclose($file);
}

//
// Unregisters a phone number from the SMS service
//
function unregister_phone_number($number, $provider)
{
	$number = add_sms_gateway($number, $provider)."\n";
	// Read all of the listed phone numbers
	$file = fopen("/var/iroom/phno", 'r');
	$numbers = array();
	while(($buffer = fgets($file)) !== false)
	{
		$numbers[] = $buffer;
	}
	// If number not in array, leave
	if(!in_array($number, $numbers)) 
	{ 
		fclose($file);
		return;
	}
	// Remove this number from array
	unset($numbers[array_search($number,$numbers)]);
	// Now, write the file
	fclose($file);
	$file = fopen("/var/iroom/phno",'w');
	foreach($numbers as $num)
	{
		fwrite($file, trim($num)."\n");
	}
	fclose($file);
}

//
// Appends the sms gateway to the phone number depending on the provider
//
function add_sms_gateway($number, $provider)
{
	switch($provider)
	{
		case 'verizon':
			$number .= '@vtext.com';
			break;
		case 'tmobile':
			$number .= '@tmomail.net';
			break;
		case 'sprint':
			$number .= '@messaging.sprintpcs.com';
			break;
		case 'att':
			$number .= '@txt.att.net';
			break;
	}
	return $number;
}

//
// Returns a pretty formatted current time
//
function get_current_time()
{
	return date("h:\\0\\0 A");
}

//
// Returns a pretty formatted current date
//
function get_current_date()
{
	return date("Y-m-d");
}

//
// Gets the GET parameters of a url in an association 
//
function get_params_of_URL($url)
{
	$params = array();
	$queryIndex = strpos($url, '?');
	// No params?
	if($queryIndex === FALSE) { return false; }
	// Trim off the URL part to get just the params
	$url = substr($url, $queryIndex+1, strlen($url)-($queryIndex+1));
	// Now, explode the params by & to separate each param
	$pvs = explode('&',$url);
	foreach($pvs as $paramValue)
	{
		// Tokenize the pair by = to separate
		$paramName = strtok($paramValue,'=');
		$paramValue = strtok('=');
		// Add to array
		$params[$paramName] = $paramValue;
		
	}
	return $params;
}

//
// Gets the value of the page (p) parameter from a URL
//
function get_page_of_params($query)
{
	$params = get_params_of_URL($query);
	if(isset($params['p']))
	{
		return $params['p'];
	}
	return false;
}

//
// Gets all of the active sensors with ID mapped to name
//
function get_active_sensors($database)
{
	$ret = array();
	$results = $database->multi_query("CALL getActiveSensors()");
	if($results)
	{
		$results = $database->use_result();
		while($row = $results->fetch_row())
		{
				$ret[$row[0]] = $row[1];
		}
		$results->free();
		if($database->more_results())
  			while($database->next_result());
	}
	return $ret;
}
?>
